package com.ctgu.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctgu.springboot.common.QueryPageParam;
import com.ctgu.springboot.common.Result;
import com.ctgu.springboot.entity.TraInfo;
import com.ctgu.springboot.entity.Word;
import com.ctgu.springboot.mapper.WordMapper;
import com.ctgu.springboot.service.StudentService;
import com.ctgu.springboot.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cs
 * @since 2023-07-06
 */
@RestController
@RequestMapping("/word")
public class WordController {
    @Autowired
    private WordService wordService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("list")//留言表
    public List<Word> list(){
        return wordService.list();
    }

    @RequestMapping("findstuid")//根据学生id查找留言
    public List<Word> list(int stu_id) {
        QueryWrapper<Word> wordQueryWrapper=new QueryWrapper<>();
        wordQueryWrapper.eq("stu_id",stu_id);
        return wordService.list(wordQueryWrapper);
    }

    @RequestMapping("listtime")//根据发言时间排序
    public List<Word> listtime(){
        QueryWrapper<Word> wordQueryWrapper=new QueryWrapper<>();
        wordQueryWrapper.last("order by word_time desc");
        return wordService.list(wordQueryWrapper);
    }

    //分页查询留言信息
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        IPage<Word> result = wordService.wordPage(query);
        return Result.suc(result.getRecords(),result.getTotal());
    }
    //获取所有留言信息
    @GetMapping("getList")
    public List<Word> getList(){
        return wordService.getList();
    }

    //新增留言
    @PostMapping("/save")
    public Result save(@RequestBody Word word){
        return wordService.save(word) ? Result.suc() :Result.fail();
    }

    //更新留言信息
    @PostMapping("/update")
    public Result update(@RequestBody Word word){
        return wordService.updateById(word) ? Result.suc() : Result.fail();
    }

    //删除留言（根据ID）
    @GetMapping("/del")
    public Result del(@RequestParam String word_id){
        return wordService.removeById(word_id) ? Result.suc() : Result.fail();
    }

    @GetMapping("getStuName")
    public String getStuName(@RequestParam Integer word_id){
        Word word = wordService.getById(word_id);
        return studentService.getById(word.getStuId()).getStuName();
    }

    //教师分页查询留言信息（没用，寄！）
    @PostMapping("/teacherlistPage")
    public Result teacherlistPage(@RequestBody QueryPageParam query){
        IPage<Word> result = wordService.teacherwordPage(query);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
